﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;

namespace FYPHP
{
    public partial class Register : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand checkUserName, cmdSearchID, cmdRegister;

                conn.Open();
                
                checkUserName = new SqlCommand("SELECT COUNT(u_id) FROM Users WHERE username=@username",conn);
                checkUserName.Parameters.AddWithValue("@username",txtUsername.Text);

                int x = Convert.ToInt32(checkUserName.ExecuteScalar());


                if (x > 0) //Repeated username
                {
                    conn.Close();
                    MessageBox("Sorry,the username is already been used.\\nPlease try again with another username.");
                    Redirect("RegisterForMember.aspx");
                }
                else   //Username Not Repeated
                {

                    String UserName = txtUsername.Text;
                    String Password = txtPassword.Text;
                    String SecretQuestion = txtSQ.Text;
                    String SecretAnswer = txtSA.Text;
                    String IC = txtIC.Text;
                    int Age = Convert.ToInt32(txtAge.Text);
                    String Gender = rblGender.SelectedValue;
                    String fullAddress = txtAdd1.Text + "|" + txtAdd2.Text + "|" + txtPostCode.Text + "|" + txtCity.Text + "|" + txtState.Text + "|" + txtCountry.Text;
                    String Email = txtEmail.Text;
                    String PhoneNo = txtPhone.Text;



                    cmdSearchID = new SqlCommand("Select MAX(u_id) from Users", conn);
                    int u_id = Convert.ToInt32(cmdSearchID.ExecuteScalar());
                    u_id++;

                    if (imgName.Text.Equals(""))
                    {
                        cmdRegister = new SqlCommand("Insert into Users Values(@Id,@Username,@Password,@Ic,@Age,@Gender,@Address,@Phone,@Status,@Email,@Points,@Position,@Secretquestion,@Secretanswer,@Picture,@Date)", conn);
                        cmdRegister.Parameters.AddWithValue("@Id", u_id);
                        cmdRegister.Parameters.AddWithValue("@Username", UserName);
                        cmdRegister.Parameters.AddWithValue("@Password", Password);
                        cmdRegister.Parameters.AddWithValue("@Ic", IC);
                        cmdRegister.Parameters.AddWithValue("@Age", Age);
                        cmdRegister.Parameters.AddWithValue("@Gender", Gender);
                        cmdRegister.Parameters.AddWithValue("@Address", fullAddress);
                        cmdRegister.Parameters.AddWithValue("@Phone", PhoneNo);
                        cmdRegister.Parameters.AddWithValue("@Status", "Activate");
                        cmdRegister.Parameters.AddWithValue("@Email", Email);
                        cmdRegister.Parameters.AddWithValue("@Points", 0);
                        cmdRegister.Parameters.AddWithValue("@Position", "Member");
                        cmdRegister.Parameters.AddWithValue("@Secretquestion", SecretQuestion);
                        cmdRegister.Parameters.AddWithValue("@Secretanswer", SecretAnswer);
                        cmdRegister.Parameters.AddWithValue("@Picture", "noimage.jpg");
                        cmdRegister.Parameters.AddWithValue("@Date", DateTime.Now);

                        cmdRegister.ExecuteNonQuery();
                    }
                    else
                    {

                        cmdRegister = new SqlCommand("Insert into Users Values(@Id,@Username,@Password,@Ic,@Age,@Gender,@Address,@Phone,@Status,@Email,@Points,@Position,@Secretquestion,@Secretanswer,@Picture,@Date)", conn);
                        cmdRegister.Parameters.AddWithValue("@Id", u_id);
                        cmdRegister.Parameters.AddWithValue("@Username", UserName);
                        cmdRegister.Parameters.AddWithValue("@Password", Password);
                        cmdRegister.Parameters.AddWithValue("@Ic", IC);
                        cmdRegister.Parameters.AddWithValue("@Age", Age);
                        cmdRegister.Parameters.AddWithValue("@Gender", Gender);
                        cmdRegister.Parameters.AddWithValue("@Address", fullAddress);
                        cmdRegister.Parameters.AddWithValue("@Phone", PhoneNo);
                        cmdRegister.Parameters.AddWithValue("@Status", "Activate");
                        cmdRegister.Parameters.AddWithValue("@Email", Email);
                        cmdRegister.Parameters.AddWithValue("@Points", 0);
                        cmdRegister.Parameters.AddWithValue("@Position", "Member");
                        cmdRegister.Parameters.AddWithValue("@Secretquestion", SecretQuestion);
                        cmdRegister.Parameters.AddWithValue("@Secretanswer", SecretAnswer);
                        cmdRegister.Parameters.AddWithValue("@Picture", imgName.Text);
                        cmdRegister.Parameters.AddWithValue("@Date", DateTime.Now);

                        cmdRegister.ExecuteNonQuery();
                    }
                    conn.Close();
                    MessageBox("Registered Successfully!");
                    Redirect("SignIn.aspx");
                }
            }
            catch (SqlException ex)
            {
                Response.Write(ex.Message);
            }


        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            string uploadFolder = Request.PhysicalApplicationPath + "/uploads/users/";

            if (FileUpload1.HasFile)
            {
                string now = String.Format("{0:ddMMyyyyHHmmss}", DateTime.Now);
                string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
                FileUpload1.SaveAs(uploadFolder + now + extension);
                UploadStatusLabel.Text = "File uploaded successfully as: ";
                imgName.Text = now + extension;
            }
            else
            {
                UploadStatusLabel.Text = "You did not specify a file to upload.";
                imgName.Text = "";
            }

            txtPassword.Attributes.Add("Value", txtPassword.Text);
            txtRetypePassword.Attributes.Add("Value", txtRetypePassword.Text);
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

    }
}